package com.esri.core.portal;

import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.tasks.e;
import com.esri.core.internal.util.d;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.Bookmark;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.WebMapQuery;
import com.esri.core.portal.WebMapLayer;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.RejectedExecutionException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.ObjectMapper;

/* loaded from: classes.dex */
public class WebMap {
    PortalItem a;
    BaseMap b;
    List<WebMapLayer> c;
    List<Bookmark> d;
    List<WebMapSubLayer> e;
    List<WebMapQuery> f;
    String g;
    String h;
    a i;
    private boolean j;

    /* loaded from: classes.dex */
    public final class a {
        double[] a;
        double b;
        double c;
        double d;

        private a() {
            WebMap.this = r1;
        }

        public double[] a() {
            return this.a;
        }

        public double b() {
            return this.b;
        }

        private double c() {
            return this.c;
        }

        private double d() {
            return this.d;
        }
    }

    WebMap() {
    }

    protected WebMap(JsonParser jsonParser, PortalItem portalItem) throws Exception {
        this.a = portalItem;
        a(jsonParser);
    }

    protected WebMap(PortalItem portalItem) throws Exception {
        this(portalItem, false);
    }

    protected WebMap(PortalItem portalItem, boolean z) throws Exception {
        this.a = portalItem;
        JsonParser jsonParser = null;
        try {
            jsonParser = d.a(portalItem.fetchData());
            jsonParser.nextToken();
            this.j = z;
            a(jsonParser);
        } finally {
            if (jsonParser != null) {
                jsonParser.close();
            }
        }
    }

    public PortalItem getInfo() {
        return this.a;
    }

    public BaseMap getBaseMap() {
        return this.b;
    }

    public List<WebMapLayer> getOperationalLayers() {
        return this.c;
    }

    public List<Bookmark> getBookmarks() {
        return this.d;
    }

    public List<WebMapQuery> getPredefinedQueries() {
        return this.f;
    }

    public String getVersion() {
        return this.g;
    }

    public String getApplicationPropertiesJson() {
        return this.h;
    }

    private a a() {
        return this.i;
    }

    public static void newInstance(final URL url, final String str, final String str2, final CallbackListener<WebMap> callbackListener) {
        try {
            e.c.execute(new Runnable() { // from class: com.esri.core.portal.WebMap.1
                @Override // java.lang.Runnable
                public void run() {
                    UserCredentials userCredentials;
                    String str3;
                    try {
                        String a2 = WebMap.a(url);
                        WebMap webMap = null;
                        String a3 = a2.contains("token=") ? WebMap.a(a2, "token=") : null;
                        if (a3 == null) {
                            String str4 = str;
                            if (str4 == null || str4.equals("") || (str3 = str2) == null || str3.equals("")) {
                                userCredentials = null;
                            } else {
                                userCredentials = new UserCredentials();
                                userCredentials.setUserAccount(str, str2);
                            }
                        } else {
                            userCredentials = new UserCredentials();
                            userCredentials.setUserToken(a3, WebMap.a(a2, "referer="));
                        }
                        PortalItem a4 = WebMap.a(a2, userCredentials);
                        CallbackListener callbackListener2 = callbackListener;
                        if (callbackListener2 != null) {
                            if (a4 != null) {
                                webMap = new WebMap(a4);
                            }
                            callbackListener2.onCallback(webMap);
                        }
                    } catch (Throwable th) {
                        CallbackListener callbackListener3 = callbackListener;
                        if (callbackListener3 != null) {
                            callbackListener3.onError(th);
                        }
                    }
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    public static void newInstance(final URL url, final UserCredentials userCredentials, final CallbackListener<WebMap> callbackListener) {
        try {
            e.c.execute(new Runnable() { // from class: com.esri.core.portal.WebMap.2
                @Override // java.lang.Runnable
                public void run() {
                    try {
                        String a2 = WebMap.a(url);
                        WebMap webMap = null;
                        String a3 = a2.contains("token=") ? WebMap.a(a2, "token=") : null;
                        if (a3 != null) {
                            userCredentials.setUserToken(a3, WebMap.a(a2, "referer="));
                        }
                        PortalItem a4 = WebMap.a(a2, userCredentials);
                        CallbackListener callbackListener2 = callbackListener;
                        if (callbackListener2 != null) {
                            if (a4 != null) {
                                webMap = new WebMap(a4);
                            }
                            callbackListener2.onCallback(webMap);
                        }
                    } catch (Throwable th) {
                        CallbackListener callbackListener3 = callbackListener;
                        if (callbackListener3 != null) {
                            callbackListener3.onError(th);
                        }
                    }
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    public static void newInstance(final String str, final Portal portal, final CallbackListener<WebMap> callbackListener) {
        try {
            e.c.execute(new Runnable() { // from class: com.esri.core.portal.WebMap.3
                @Override // java.lang.Runnable
                public void run() {
                    try {
                        PortalItem fetchItem = PortalItem.fetchItem(portal, str);
                        CallbackListener callbackListener2 = callbackListener;
                        if (callbackListener2 != null) {
                            callbackListener2.onCallback(fetchItem == null ? null : new WebMap(fetchItem));
                        }
                    } catch (Throwable th) {
                        CallbackListener callbackListener3 = callbackListener;
                        if (callbackListener3 != null) {
                            callbackListener3.onError(th);
                        }
                    }
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    public static WebMap newInstance(String str, Portal portal) throws Exception {
        return newInstance(PortalItem.fetchItem(portal, str));
    }

    public static WebMap newInstance(PortalItem portalItem) throws Exception {
        if (portalItem == null) {
            return null;
        }
        return new WebMap(portalItem);
    }

    public static WebMap newInstance(JsonParser jsonParser, Envelope envelope, SpatialReference spatialReference) throws Exception {
        if (jsonParser == null) {
            return null;
        }
        PortalItem portalItem = new PortalItem(null);
        portalItem.h = PortalItemType.WEBMAP;
        if (envelope != null && spatialReference != null && spatialReference.getID() != 4326) {
            envelope = (Envelope) GeometryEngine.project(envelope, spatialReference, SpatialReference.create(4326));
        }
        portalItem.m = envelope;
        return new WebMap(jsonParser, portalItem);
    }

    public static WebMap newInstance(BaseMap baseMap, Envelope envelope, SpatialReference spatialReference) {
        if (baseMap == null) {
            return null;
        }
        WebMap webMap = new WebMap();
        webMap.b = baseMap;
        if (envelope != null && !envelope.isEmpty() && spatialReference != null && spatialReference.getID() != 4326) {
            envelope = (Envelope) GeometryEngine.project(envelope, spatialReference, SpatialReference.create(4326));
        }
        PortalItem portalItem = new PortalItem(null);
        portalItem.m = envelope;
        webMap.a = portalItem;
        return webMap;
    }

    protected static WebMap newInstance(String str, Portal portal, boolean z) throws Exception {
        PortalItem fetchItem = PortalItem.fetchItem(portal, str);
        if (fetchItem == null) {
            return null;
        }
        return new WebMap(fetchItem, z);
    }

    static String a(String str, String str2) {
        StringTokenizer stringTokenizer = new StringTokenizer(str, "?");
        String str3 = null;
        if (stringTokenizer.countTokens() == 2) {
            stringTokenizer.nextToken();
            String nextToken = stringTokenizer.nextToken();
            if (nextToken.contains("%26")) {
                nextToken = nextToken.replace("%26", "&");
            }
            if (nextToken.contains("&")) {
                StringTokenizer stringTokenizer2 = new StringTokenizer(nextToken, "&");
                while (stringTokenizer2.hasMoreTokens()) {
                    String nextToken2 = stringTokenizer2.nextToken();
                    if (nextToken2.contains(str2)) {
                        str3 = nextToken2.substring(nextToken2.indexOf("=") + 1, nextToken2.length());
                    }
                }
                return str3;
            }
            return nextToken.substring(nextToken.indexOf("=") + 1, nextToken.length());
        }
        return null;
    }

    private void a(JsonParser jsonParser) throws Exception {
        PortalItem portalItem;
        WebMap webMap;
        WebMap webMap2;
        if (jsonParser == null) {
            return;
        }
        jsonParser.enable(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if (currentName == null || currentName.trim().length() == 0) {
                break;
            } else if ("applicationProperties".equals(currentName)) {
                this.h = new ObjectMapper().readTree(jsonParser).toString();
            } else if ("operationalLayers".equals(currentName)) {
                this.c = new ArrayList();
                if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        this.c.add(WebMapLayer.fromJson(jsonParser));
                    }
                }
            } else if ("layers".equals(currentName)) {
                this.e = new ArrayList();
                if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        this.e.add(WebMapSubLayer.a(jsonParser));
                    }
                }
            } else if ("baseMap".equals(currentName)) {
                this.b = BaseMap.fromJson(jsonParser);
            } else if ("version".equals(currentName)) {
                this.g = jsonParser.getText();
            } else if ("tasks".equals(currentName)) {
                if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
                    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                        if ("queryTasks".equals(jsonParser.getCurrentName())) {
                            this.f = new ArrayList();
                            if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                                    this.f.add(WebMapQuery.fromJson(jsonParser));
                                }
                            }
                        }
                    }
                }
            } else if ("bookmarks".equals(currentName)) {
                this.d = new ArrayList();
                if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        this.d.add(Bookmark.fromJson(jsonParser));
                    }
                }
            } else if ("mapOptions".equals(currentName)) {
                this.i = new a();
                while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                    String currentName2 = jsonParser.getCurrentName();
                    jsonParser.nextToken();
                    if ("time".equals(currentName2)) {
                        ArrayList arrayList = new ArrayList();
                        if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                                arrayList.add(Double.valueOf(jsonParser.getDoubleValue()));
                            }
                        }
                        this.i.a = new double[arrayList.size()];
                        for (int i = 0; i < arrayList.size(); i++) {
                            this.i.a[i] = ((Double) arrayList.get(i)).doubleValue();
                        }
                    } else if ("utcOffset".equals(currentName2)) {
                        this.i.b = jsonParser.getDoubleValue();
                    } else if ("minScale".equals(currentName2)) {
                        this.i.c = jsonParser.getDoubleValue();
                    } else if ("maxScale".equals(currentName2)) {
                        this.i.d = jsonParser.getDoubleValue();
                    } else {
                        jsonParser.skipChildren();
                    }
                }
            } else {
                jsonParser.skipChildren();
            }
        }
        HashMap hashMap = new HashMap();
        if (this.c != null && (portalItem = this.a) != null && portalItem.a != null) {
            for (WebMapLayer webMapLayer : this.c) {
                List<WebMapSubLayer> list = this.e;
                if (list == null || list.isEmpty()) {
                    if (webMapLayer.s == null && (webMapLayer.t == null || webMapLayer.t.isEmpty())) {
                        if (webMapLayer != null && webMapLayer.f != null && webMapLayer.getType() != WebMapLayer.Type.KML && webMapLayer.f.trim().length() > 0 && !hashMap.containsKey(webMapLayer.f)) {
                            try {
                                webMap2 = newInstance(webMapLayer.f, this.a.a, true);
                            } catch (Exception e) {
                                e.printStackTrace();
                                webMap2 = null;
                            }
                            if (webMap2 != null) {
                                hashMap.put(webMapLayer.f, webMap2);
                            }
                        }
                    }
                }
            }
            if (!hashMap.isEmpty()) {
                for (WebMapLayer webMapLayer2 : this.c) {
                    if (webMapLayer2.f != null && webMapLayer2.f.trim().length() > 0 && (webMap = (WebMap) hashMap.get(webMapLayer2.f)) != null) {
                        String url = webMapLayer2.getUrl();
                        if (webMap.e != null && url != null) {
                            if (url.contains("/MapServer") && !url.matches(".+/MapServer/\\d+.*")) {
                                webMapLayer2.t = webMap.e;
                                if (webMapLayer2.t != null && webMapLayer2.t.size() > 0 && webMapLayer2.s == null) {
                                    webMapLayer2.i = new ArrayList();
                                    for (WebMapSubLayer webMapSubLayer : webMapLayer2.t) {
                                        if (webMapSubLayer.getLayerId() > -1) {
                                            if (webMapSubLayer.getWebMapPopupInfo() != null) {
                                                if (webMapSubLayer.getLayerURL() != null && webMapSubLayer.getLayerURL().trim().length() > 0) {
                                                    webMapSubLayer.getWebMapPopupInfo().setLayerUrl(webMapSubLayer.getLayerURL());
                                                } else {
                                                    webMapSubLayer.getWebMapPopupInfo().setLayerUrl(webMapLayer2.getUrl() + "/" + webMapSubLayer.getLayerId());
                                                }
                                            }
                                            if (!webMapSubLayer.a()) {
                                                webMapSubLayer.f = webMapLayer2.isShowLegend();
                                            }
                                            if (webMapSubLayer.c != -1 && !webMapSubLayer.isShowLegend()) {
                                                webMapLayer2.i.add(Integer.valueOf(webMapSubLayer.getLayerId()));
                                            }
                                        }
                                    }
                                }
                            } else {
                                Iterator<WebMapSubLayer> it = webMap.e.iterator();
                                while (true) {
                                    if (!it.hasNext()) {
                                        break;
                                    }
                                    WebMapSubLayer next = it.next();
                                    if (next != null && next.c > -1) {
                                        if (url.endsWith("/" + next.c)) {
                                            if (next.a != null && webMapLayer2.p == null) {
                                                webMapLayer2.p = next.a;
                                            }
                                            if (next.e != null && webMapLayer2.s == null) {
                                                webMapLayer2.s = next.e;
                                                if (next.getLayerURL() != null && next.getLayerURL().trim().length() > 0) {
                                                    next.getWebMapPopupInfo().setLayerUrl(next.getLayerURL());
                                                } else {
                                                    next.getWebMapPopupInfo().setLayerUrl(webMapLayer2.getUrl() + "/" + next.getLayerId());
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (this.a.getType() != PortalItemType.WEBMAP && !this.j) {
            throw new InvalidPortalItemIdException("Given portal item is not a web map");
        }
    }

    static String a(URL url) {
        String url2 = url.toString();
        if (url2.contains("%3D")) {
            url2 = url2.replace("%3D", "=");
        } else if (url2.contains("%3d")) {
            url2 = url2.replace("%3d", "=");
        }
        if (url2.contains("%3F")) {
            return url2.replace("%3F", "?");
        }
        return url2.contains("%3f") ? url2.replace("%3f", "?") : url2;
    }

    static PortalItem a(String str, UserCredentials userCredentials) throws Exception {
        String str2;
        String str3 = "/sharing";
        if (str.contains("?id=")) {
            str2 = a(str, "id=");
        } else if (str.contains("?webmap=")) {
            str2 = a(str, "webmap=");
        } else if (str.contains("?services=")) {
            str2 = a(str, "services=");
        } else {
            if (str.contains("/sharing/content/items/")) {
                str2 = str.substring(str.indexOf("/sharing/content/items/") + 23);
                if (str2 != null && str2.contains("?")) {
                    str2 = str2.substring(0, str2.indexOf("?"));
                }
            } else if (str.contains("/sharing/rest/content/items/")) {
                str2 = str.substring(str.indexOf("/sharing/rest/content/items/") + 28);
                if (str2 != null && str2.contains("?")) {
                    str2 = str2.substring(0, str2.indexOf("?"));
                }
            } else {
                str2 = null;
            }
            return PortalItem.fetchItem(new Portal(str.substring(0, str.lastIndexOf(str3)), userCredentials), str2);
        }
        str3 = "/home";
        return PortalItem.fetchItem(new Portal(str.substring(0, str.lastIndexOf(str3)), userCredentials), str2);
    }

    public UserCredentials getCredentials() {
        PortalItem portalItem = this.a;
        if (portalItem == null || portalItem.a == null) {
            return null;
        }
        return this.a.a.getCredentials();
    }

    public Envelope getInitExtent() {
        PortalItem portalItem = this.a;
        if (portalItem == null) {
            return null;
        }
        return portalItem.getExtent();
    }
}
